/*******************************************************************************
*  file    :
*  created : 25.11.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "qloggercolorconsoleappender.hpp"
#include "console/consolecolor.hpp"

QLoggerColorConsoleAppender::QLoggerColorConsoleAppender(int level, FILE* stream, const QString& format, const QString& time_format) :
    QLoggerConsoleAppender(level, stream, format, time_format)
{
    initColors();
    setLevelColor(LevelNone      , ColorGreen);
    setLevelColor(LevelDebug     , ColorCyan);
    setLevelColor(LevelInfo      , ColorWhite);
    setLevelColor(LevelWarn      , ColorYellow);
    setLevelColor(LevelError     , ColorRed);
    setLevelColor(LevelCritical  , ColorRed);
    setLevelColor(LevelFatal     , ColorRed);
    setLevelColor(LevelException , ColorRed);
}

QLoggerColorConsoleAppender::~QLoggerColorConsoleAppender()
{
    /*initColors();
    setColor();
    if(out_)
        (*out_) << "xxx yyy   ";*/
}

void QLoggerColorConsoleAppender::write(const QLoggerEvent &event)
{
    QMutexLocker lock(&mutex);
    setFGColor(levelColors_[event.level]);
    streamWrite( (*out_) , event);
    setFGColor(ColorWhite);
}

void QLoggerColorConsoleAppender::setLevelColor(QLoggerLevel lvl, short color)
{
    levelColors_[lvl] = color;
}

void QLoggerColorConsoleAppender::setColor()
{
#ifdef _WIN32
    SetConsoleTextAttribute(hConsole, FGColor | BGColor);
#else
    std::string clr = "\033[";
    clr += BGColor;
    clr += ";";
    clr += FGColor;
    clr += "m";
    (*out_) << clr.c_str();
    out_->flush();
#endif
}

void QLoggerColorConsoleAppender::setFGColor(const short color)
{
    #ifdef _WIN32
    int val;
    switch(color)
        {
        default:
        case ColorBlack:
            val = 0;
        break;
        case ColorBlue:
            val = FOREGROUND_BLUE;
        break;
        case ColorCyan:
            val =  FOREGROUND_BLUE|FOREGROUND_GREEN;
        break;
        case ColorGreen:
            val = FOREGROUND_GREEN;
        break;
        case ColorMagenta:
            val = FOREGROUND_BLUE|FOREGROUND_RED;
        break;
        case ColorRed:
            val = FOREGROUND_RED;
        break;
        case ColorWhite:
            val = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED;
        break;
        case ColorYellow:
            val = FOREGROUND_GREEN|FOREGROUND_RED;
        break;
        }
    FGColor = val;
    #else
    std::string val;
    switch(color)
        {
        case ColorBlack:
            val = "30";
        break;
        case ColorBlue:
            val = "34";
        break;
        case ColorCyan:
            val = "36";
        break;
        case ColorGreen:
            val = "32";
        break;
        case ColorMagenta:
            val = "35";
        break;
        case ColorRed:
            val = "31";
        break;
        default:
        case ColorWhite:
            val = "37";
        break;
        case ColorYellow:
            val = "33";
        break;
        }
    FGColor = val;
    #endif
    setColor();
}

void QLoggerColorConsoleAppender::initColors()
{
    //Setting the default color
    //if platform is Windows
    #ifdef _WIN32
        //set default values
        BGColor  = 0;
        FGColor  = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED;
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    //if the plaform is other than Windows
    #else
        BGColor = "30";
        FGColor = "47";
    #endif
}

